/*

    This file is part of Blue Violin.

    Blue Violin is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Blue Violin is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero Public License for more details.

    You should have received a copy of the GNU Affero Public License
    along with Blue Violin.  If not, see <http://www.gnu.org/licenses/>.

    Blue Violin  Copyright (C) 2008  Blue Violin, Inc. & Josh Watts <josh.watts@blueviolin.org>
	For more information, visit http://www.blueviolin.org/

    This program comes with ABSOLUTELY NO WARRANTY.

*/

function WindowResizer()
{
}

WindowResizer.getAbsoluteTop = function(objElement)
{
	var nTotal = 0;
	if (null == objElement)
	{
		return 0;
	}
	nTotal = objElement.offsetTop + this.getAbsoluteTop(objElement.offsetParent);
	return nTotal;
	// alert(objElement.offsetTop);
}


WindowResizer.getAbsoluteLeft = function(objElement)
{
	var nTotal = 0;
	if (null == objElement)
	{
		return 0;
	}
	nTotal = objElement.offsetLeft + this.getAbsoluteLeft(objElement.offsetParent);
	return nTotal;
}

WindowResizer.resize = function()
{
	// debugger;
	window.resizeEnd = (window.resizeEnd == null) ? {} : window.resizeEnd;
	$get("spnActionTree").style.height = 0 + "px";
	var spnBrowsers = $get("spnBrowsers");
	$get("spnActionTree").style.height = "0px";
	$get("spnActionTree").style.width = "0px";
	for (var i = 0;
		 i < spnBrowsers.children.length;
		 ++i)
	{
		var oChild = spnBrowsers.children(i);
		oChild.style.height = "0px";
		oChild.style.width = "0px";
		if ("divMainBrowser" != oChild.id)
		{
			oChild.style.top = WindowResizer.getAbsoluteTop($get("divMainBrowser")) + "px";
			oChild.style.left = WindowResizer.getAbsoluteLeft($get("divMainBrowser")) + "px";
		}
		for (var j = 0;
			 j < oChild.children.length;
			 ++j)
		{
			var oGrandchild = oChild.children(j);
			oGrandchild.style.height = "0px";
			oGrandchild.style.width = "0px";
		}
	}
	window.clearTimeout(WindowResizer.resizeAction);
	window.resizeEnd = window.setTimeout(WindowResizer.resizeAction, 20);
}

WindowResizer.resizeAction = function()
{
	var nBrowserHeight = WindowResizer.get_browserHeight();
	var nTreeWidth = WindowResizer.get_treeWidth();
	var nBrowserWidth = WindowResizer.get_browserWidth();
	var spnBrowsers = $get("spnBrowsers");
	var nBrowserTop = WindowResizer.getAbsoluteTop($get("divMainBrowser"));
	var nBrowserLeft = WindowResizer.getAbsoluteLeft($get("divMainBrowser"));
	var divReport = null;
	var divBrowserSnapshot = null;
	var oChild = null;
	var oGrandchild = null;

	$get("spnActionTree").style.height = nBrowserHeight + "px";
	$get("spnActionTree").style.width = nTreeWidth + "px";


	for (var i = 0;
		 i < spnBrowsers.children.length;
		 ++i)
	{

		oChild = spnBrowsers.children(i);
		oChild.style.height = nBrowserHeight + "px";
		oChild.style.width = nBrowserWidth + "px";
		oChild.style.top = nBrowserTop + "px";
		oChild.style.left = nBrowserLeft + "px";

		for (var j = 0;
			 j < oChild.children.length;
			 ++j)
		{
			oGrandchild = oChild.children(j);
			oGrandchild.style.height = nBrowserHeight + "px";
			oGrandchild.style.width = nBrowserWidth + "px";
		}
	}

	divReport = $get("divReport");
	divBrowserSnapshot = $get("divBrowserSnapshot");
	divBrowserSnapshot.style.height = divReport.style.height = nBrowserHeight + "px";
	divBrowserSnapshot.style.width = divReport.style.width = (nBrowserWidth - 2) + "px";
	divBrowserSnapshot.style.top = divReport.style.top = WindowResizer.getAbsoluteTop($get("spnActionTree"));
	divBrowserSnapshot.style.left = divReport.style.left = (nTreeWidth + 10) + "px";

	BVReport.resize();

	var txtToolbarAddr = $get("txtToolbarAddress");
	txtToolbarAddr.style.width = (document.body.clientWidth - (30 * 7) - 30 - 75) + "px";
}

WindowResizer.get_treeWidth = function()
{
	return Math.floor(document.body.clientWidth * .2);
}


WindowResizer.get_browserWidth = function()
{
	var reVersion = /MSIE 6\.0/;
	if (navigator.userAgent.match(reVersion))
	{
		return document.body.clientWidth - this.get_treeWidth() - 20;
	}
	else
	{
		return document.body.clientWidth - this.get_treeWidth() - 16;
	}
}


WindowResizer.get_browserHeight = function()
{
	return document.body.clientHeight - $get("divWorkspace").offsetTop - 6;
}
